<template>
  <div id="Know">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>体育锻炼知识管理</el-breadcrumb-item>
      <el-breadcrumb-item>体育锻炼知识</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索框 -->
      <el-row :gutter="21" class="opt_div">
        <el-col :span="4">
          <el-input placeholder="项目名称" v-model="exercisesName"  size="mini"></el-input>
        </el-col>
        <el-col :span="4">
          <el-select v-model="status" placeholder="请选择状态" size="mini">
            <el-option label="全部" value=""></el-option>
            <el-option label="已发布" value="1"></el-option>
            <el-option label="已撤回" value="2"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="mini" @click="searchExercise">查询</el-button>
        </el-col>
        <el-col :span="4" class="div_btn">
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addExercise" size="mini">新增</el-button>
        </el-col>
      </el-row>
      <!--内容面板-->
      <el-table :data="exerciseList" style="width: 100%">
        <el-table-column prop="exercisesName" label="项目名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="exercisesContent" label="项目内容" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="exercisesGood" label="收获" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="photo" label="项目图片" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-image
                style="width: 120px; height: 100px"
                :src="scope.row.photo"
                fit="contain">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="发布状态" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span v-if="scope.row.status==2"><el-tag type="danger">已撤回</el-tag></span>
            <span v-else> <el-tag size="small">已发布</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="workName" label="发布人员" >
          <template slot-scope="scope">
            <span>{{scope.row.workName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="success"  circle size="mini" @click="updatExercises(scope.row.exercisesId)">编辑</el-button>
            <el-button type="danger"  circle @click="retract(scope.row.exercisesId)" size="mini">撤回</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          small
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[3, 5, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>

  </div>
</template>
<script>
export default {
  name: "KnowledgeManager",
  data(){
    return{
      exerciseList:[],
      exercisesName:"",
      exercisesContent:"",
      exercisesGood:"",
      status:"",
      photo:"",
      pageIndex:1,
      total:10,
      pageSize:3,
    }
  },
  methods:{
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getExerciseList(this.exercisesName,this.status,this.pageIndex,this.pageSize)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.getExerciseList(this.exercisesName,this.status,this.pageIndex,this.pageSize)
    },

    //查询锻炼知识
    getExerciseList(exercisesName,status,pageIndex,pageSize){
      var params = new URLSearchParams();
      params.append("exercisesName",exercisesName)
      params.append("status",status)
      params.append("pageIndex",pageIndex)
      params.append("pageSize",pageSize)
      this.$axios.post('/exercises-info/getExercise',params).then(res=>{
        console.log("锻炼"+res)
        this.exerciseList=res.data.data.list
        this.total=res.data.data.total
      }).catch(err=>{
        console.log(err)
      })
    },
    searchExercise(){
      this.pageIndex=1
      this.getExerciseList(this.exercisesName,this.status,this.pageIndex,this.pageSize)
    },

    //发布人员
    getWorkId(){
      this.$axios.post("/user-info/getWorkId").then(res=>{
        console.log(res)
        this.workIdList=res.data.data
      })
    },

    addExercise(){
      this.$router.push("/exercise/AddExercise")
    },

    updatExercises(exercisesId){
      console.log(exercisesId)
      this.$router.push({name:"UpdatExercise",params:{exercisesId:exercisesId}})
    },

    //撤回
    retract(exercisesId){
      this.$axios.post('/exercises-info/retract',"exercisesId="+exercisesId).then(res=>{
        if (res.data.code ==200){
          this.$message.success("撤回成功")
          this.getExerciseList(this.exercisesName,this.status,this.pageIndex,this.pageSize)
          console.log(res)
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(err=>{
        console.log(err)
      })
    },


  },
  created() {
    this.getExerciseList(this.exercisesName,this.status,this.pageIndex,this.pageSize)
    this.getWorkId()
  }
}
</script>

<style scoped>
.el-breadcrumb{
  margin-bottom: 20px;
}
.el-table{
  margin-top: 20px;
  margin-bottom: 20px;
}
.search_input{
  margin-left: 10px;
}
.div_btn{
  margin-left: -132px;
}
</style>
